<?php
/**
* 用户weixin控制器
*/
class Index extends MY_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('User_model', 'user_mdl');
		$this->load->model('Step_model','step_mdl');
	}
	/**
	 * 用户首页
	 * @DateTime 2017-09-04
     * @author xyl
	 * @return [type] [description]
	 */
	public function index()
	{
		// $post = file_get_contents("php://input");
		// $post = json_decode($post,true);
		$post['user_id'] = 2;
		$data['info'] = $this->user_mdl->getUser(array('id'=>$post['user_id']));
		$data['step'] = $this->step_mdl->get_today($post['user_id']);
		$data['jsapi'] = array(
			'appId'	=>	'1',
			'timestamp'=>2,
			'nonceStr'=>1,
			'signature'	=>1,
		);
		$this->load->view('weixin/index',$data);
	}

	/**
	 * 今天的步数
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	function step()
	{
		$post['user_id'] = 2;
		$data['info'] = $this->user_mdl->getUser(array('id'=>$post['user_id']));
		$data['step'] = $this->step_mdl->get_today($post['user_id']);
		$info = $this->step_mdl->get_sort($post['user_id']);
		$data = array_merge($data,$info);
		$this->load->view('weixin/step',$data);
	}

	/**
	 * 分享页
	 * @author XYL
	 * @DateTime 2018-06-06
	 * @return   [type]     [description]
	 */
	function share()
	{
		$post['user_id'] = 2;
		$data['info'] = $this->user_mdl->getUser(array('id'=>$post['user_id']));
		$add_list = array(
			'user_id'	=>	$data['info']['id'],
			'openid'	=>	$data['info']['openid'],
		);
		$this->load->model('QRList_model','qrlist_mdl');
		$this->qrlist_mdl->addlist($add_list);
		$data['step'] = $this->step_mdl->get_today($post['user_id']);
		// $info = $this->step_mdl->get_sort($post['user_id']);
		// $data = array_merge($data,$info);
		$this->load->view('weixin/share',$data);
	}

	/**
	 * 我的记录
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @param    integer    $type [description]
	 * @return   [type]           [description]
	 */
	function record($type = 1)
	{
		$post['user_id'] = 2;
		$data['record'] = $this->step_mdl->get_record($post['user_id'],$type);
		$data['type'] = $type;
		$this->load->view('weixin/record',$data);
	}

	/**
	 * 个人设置
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	function setting()
	{
		$post['user_id'] = 2;
		$data['info'] = $this->user_mdl->getUser(array('id'=>$post['user_id']));
		$this->load->view('weixin/setting',$data);
	}

	/**
	 * 改变用户设置
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function change_online()
	{
		$post = $this->input->post();
		$res = $this->user_mdl->change_online($post);
		echo json_encode($res);return;
	}

	/**
	 * 我的积分
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	function credit()
	{
		$post['user_id'] = 2;
		$data['info'] = $this->user_mdl->getUser(array('id'=>$post['user_id']));
		$this->load->model('Credit_model','credit_mdl');
		// $data['list'] = $this->
		$this->load->view('weixin/credit',$data);
	}


	/**
	 * 添加用户信息
	 * @DateTime 2017-07-06
	 * @author zyl
	 */
	function addUserInfo(){
		if($_POST){
			$post = $this->input->post();
			$post['from'] = 'wx';
			$res = $this->user->userInsert($post);
			if($res){
				redirect(base_url().'Weixin/User/infoSuccess');
			}
			
		}
		$data['user_id'] = $_SESSION['userinfo']['id'];
		// $data['title'] = '信息填写';
    	// $this->load->view('weixin/common/header',$data);
		$this->load->view('weixin/user/addUserInfo',$data);
	}

    /**
     * 添加微信上传附件
     * @DateTime 2017-08-09
     * @author zyl
     */
    function addWeixinImgs()
    {
    	$post = $this->input->post();

    	$where['id'] = $_SESSION['userinfo']['id'];

    	$data['wx_imgs'] = $post['url'];

    	$return = $this->user->updateAppUser($data,$where);

    	echo json_encode($return);
    }

    /**
     * 上传附件图
     * @DateTime 2017-08-09
     * @author zyl
     * @return   [type]     [description]
     */
    function uploadImg (){
    	$where['id'] = $_SESSION['userinfo']['id'];
    	if($_POST){
    		$post = $this->input->post();
			// print_r($post);die();
    		$data['contract_imgs'] = isset($post['contract'])?json_encode($post['contract']):'';
    		$data['receipt_imgs'] = isset($post['receipt'])?json_encode($post['receipt']):'';
    		if(!empty($post)){
    			$res = $this->user->updateAppUser($data,$where);
    		}
    		redirect(base_url().'Weixin/User/infoSuccess');
    	}else{
			//微信设置
    		$this->load->model('Token_model','token');
    		$data['jsapi'] = $this->token->getSignPackage();

    		$user = $this->user->getUser($where);
    		$data['receipt'] = json_decode($user['receipt_imgs'],true);
    		$data['contract'] = json_decode($user['contract_imgs'],true);
    		$this->load->view('weixin/user/upload',$data);
    	}

    }

    /**
     * 检测用户状态
     * @DateTime 2017-08-23
     * @author zyl
     * @return   [type]     [description]
     */
    function checkUserStatus()
    {
    	header("Access-Control-Allow-Origin: *");

    	if (isset($_SESSION['userinfo']) && isset($_SESSION['userinfo']['credit_status'])) {
    		$return = array('code'=>1,'msg'=>'已申请');
    	} else {
    		$return = array('code'=>0,'msg'=>'未申请');
    	}

    	echo json_encode($return);
    }


	/**
	 * 信息提交成功界面
	 * @DateTime 2017-07-06
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function infoSuccess(){
		$where['id'] = $_SESSION['userinfo']['id'];
		$user_info = $this->user->getUser($where);
		// $user_info['credit_status'] = 3;
		switch ($user_info['credit_status']) {
			case 2:
			$url = base_url().'Weixin/User/checkFinish';
			break;
			case 3:
			$url = base_url().'Weixin/User/checkInstallment/1';
			break;
			case 1:
			$url = base_url().'Weixin/User/checkInstallment';
			break;
			
			default:
			$url = base_url().'Weixin/User/submitSuccess';
			break;
		}
		redirect($url);
		
		$this->load->view('weixin/user/success');
	}
	
    /**
     * 申请成功
     * @DateTime 2017-08-23
	 * @author xyl
     * @return [type] [description]
     */
    function submitSuccess(){
    	$this->load->view("weixin/user/success");
    }

	/**
	 * 授信成功
	 * @DateTime 2017-08-23
	 * @author xyl
	 * @return [type] [description]
	 */
	function checkFinish(){
		$where['id'] = $_SESSION['userinfo']['id'];
		$user = $this->user->getUser($where);
		$vdata['info'] = $user;
		$this->load->view('weixin/user/checkFinish',$vdata);
	}

	/**
	 * 审核中
	 * @DateTime 2017-08-16
	 * @author xyl
	 * @param  boolean $type [如果存在则显示重新申请页面，若无则为审核中]
	 * @return [type]        [description]
	 */
	function checkInstallment($type = false)
	{
		if (!empty($type)) {
			$vdata['type'] = 1;
		}
		$where['id'] = $_SESSION['userinfo']['id'];
		$vdata['info'] = $this->user->getUser($where);
		$this->load->view('weixin/user/check',$vdata);
	}

	/**
	 * 重新申请分期
	 * @DateTime 2017-08-23
	 * @author xyl
	 * @return [type] [description]
	 */
	public function resetStatus()
	{
		$res = $this->user->resetStatus();
		echo json_encode($res);return;
	}
}
